package io.mft.commons.function.response.kv;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.PageUtil;
import cn.hutool.db.Page;
import cn.hutool.json.JSONUtil;
import io.mft.commons.function.FunctionResponseType;
import io.mft.commons.function.IFunctionResponse;
import lombok.Getter;
import lombok.Setter;

public class PageObject<T> implements Serializable, IFunctionResponse {

	@Getter
	@Setter
	public static class PageMetadata {
		/**
		 * 总记录数
		 */
		private int totalCount;
		/**
		 * 每页记录数
		 */
		private int pageSize;
		/**
		 * 总页数
		 */
		private int totalPage;

		/**
		 * 当前页数
		 */
		private int currPage;
	}

	/**
	 * @说明：TODO
	 * @字段名称 serialVersionUID
	 */

	private static final long serialVersionUID = -5728071623378652861L;

	private List<T> list = new ArrayList<T>();

	public static final int DEFAULT_PAGE_SIZE = Page.DEFAULT_PAGE_SIZE;

	public List<T> getList() {
		return list;
	}

	/**
	 * 总记录数
	 */
	private int totalCount;
	/**
	 * 每页记录数
	 */
	private int pageSize;
	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 当前页数
	 */
	private int currPage;

	/**
	 * 构造
	 */
	public PageObject() {
	}

	/**
	 * 构造
	 *
	 * @param page     页码，0表示第一页
	 * @param pageSize 每页结果数
	 */
	public PageObject(int currPage, int pageSize) {
		this.currPage = Math.max(currPage, 0);
		this.pageSize = pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize;
	}

	public PageMetadata pageMetadata() {
		PageMetadata metadata = new PageMetadata();
		metadata.setCurrPage(currPage);
		metadata.setPageSize(pageSize);
		metadata.setTotalCount(totalCount);
		metadata.setTotalPage(totalPage);
		return metadata;
	}

	/**
	 * 构造
	 *
	 * @param page     页码，0表示第一页
	 * @param pageSize 每页结果数
	 * @param total    结果总数
	 */
	public PageObject(int page, int pageSize, int totalCount) {
		this(page, pageSize);
		this.totalCount = totalCount;
		this.totalPage = PageUtil.totalPage(totalCount, pageSize);
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	/**
	 * @return 每页结果数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页结果数
	 *
	 * @param pageSize 每页结果数
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return 总页数
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 设置总页数
	 *
	 * @param totalPage 总页数
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * @return 是否第一页
	 */
	public boolean first() {
		return this.currPage == 0;
	}

	/**
	 * @return 是否最后一页
	 */
	public boolean last() {
		return this.currPage >= (this.totalPage - 1);
	}

	@Override
	public FunctionResponseType responseType() {
		return FunctionResponseType.kv;
	}
	
	@Override
	public String toString() {
		return JSONUtil.toJsonStr(this);
	}
}
